<?php

namespace app\admin\controller\pay;

use app\common\controller\Backend;

class Pay extends Backend
{
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Pay.Pay');
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 补单
    public function fill($ids = null) {
        $row = $this->model->get($ids);

        if (!$row)
        {
            $this->error(__('订单不存在'));
        }

        if ($this->request->isPost()) {

            if($row['status']!= 0) {
                $this->error(__('订单状态不正确或订单已关闭'));
            }

            // 修改订单状态
            $result = $this->model->where('id', $ids)->update(['status' => 1]);
            if ($result === false)
            {
                $this->error("订单状态修改失败：".$this->model->getError());
                exit;
            }
            $this->success("补单成功");

        }
    }

    
    // 删除
    public function del($ids = null)
    {
        $row = $this->model->select($ids);

        if (!$row)
        {
            $this->error(__('订单不存在'));
        }

        $result = $this->model->destroy($ids);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }

    // 回收站列表
    public function recyclebin()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

    // 还原
    public function restore($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('订单不存在'));
        }

        $result = $this->model
            ->onlyTrashed()
            ->where('id', 'IN', $ids)
            ->update(['deletetime' => null]);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }

    // 销毁
    public function destroy($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('订单不存在'));
        }

        $result = $this->model->destroy($ids, true);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        // 删除收款码图片
        foreach($row as $item)
        {
            @is_file("." . $item['wxcode']) && @unlink("." . $item['wxcode']);
            @is_file("." . $item['zfbcode']) && @unlink("." . $item['zfbcode']);
        }

        $this->success();

    }
}
